package edu.its.dreamteam.web.pages.admin;

import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry.ComponentResources;
import org.apache.tapestry.annotations.ApplicationState;
import org.apache.tapestry.annotations.Retain;
import org.apache.tapestry.beaneditor.BeanModel;
import org.apache.tapestry.ioc.annotations.Inject;
import org.apache.tapestry.services.BeanModelSource;

import edu.its.dreamteam.daf.beans.Formation;
import edu.its.dreamteam.daf.dao.IFormationDao;
import edu.its.dreamteam.web.base.SecureBasePage;

public class FormationGrid extends SecureBasePage{
	@Inject
	private IFormationDao formationDao;
	private List<Formation> formationList;
	
	@Retain
	private BeanModel formationModel;
	@Inject
	private BeanModelSource positionModelSource;
    @Inject
    private ComponentResources resources;   
    
    @ApplicationState	
    private Formation formation;
    
    private boolean delete;
    private List<String> deletedList;
    
    public boolean isDelete() {
		return delete;
	}
	public void setDelete(boolean delete) {
		if(delete){
			getDeletedList().add(getFormation().getIdFormation());
		}
		this.delete = delete;
	}
	public List<String> getDeletedList() {
		if(deletedList==null){
			deletedList = new ArrayList<String>();
		}
		return deletedList;
	}
	
	
	public Formation getFormation() {
		return formation;
	}
	public void setFormation(Formation formation) {
		this.formation = formation;
	}
	public IFormationDao getFormationDao() {
		return formationDao;
	}
	
	public List<Formation> getFormationList() {
		if(null==formationList){
			formationList = new ArrayList<Formation>();			
		}
		formationList = getFormationDao().queryForList("", "");
		return formationList;
	}
	
	void pageLoaded(){
		formationModel = positionModelSource.create(Formation.class, false, resources);
		formationModel.add("actions", null).label("Edit");	
		formationModel.get("formation").label("test");
		
    }	
	Object onSuccessFromPositionForm(){
		for(String element : getDeletedList()){
			setFormation((Formation) getFormationDao().findByPrimaryKey(element));
			getFormationDao().delete(getFormation());
		}
		return PositionGrid.class;
	}
	public BeanModel getFormationModel() {
		return formationModel;
	}
	
	
}
